package com.example.administrator.u17.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.RadioGroup;

import com.example.administrator.u17.fragment.BookFraqment;
import com.example.administrator.u17.fragment.FirstFragment;
import com.example.administrator.u17.fragment.MineFragment;
import com.example.administrator.u17.fragment.SeachFragment;
import com.example.administrator.u17.R;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup rg;
    private FragmentManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        setListener();
    }

    private void initData() {
        manager=getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        FirstFragment firstFragment=new FirstFragment();
        transaction.add(R.id.ll_main,firstFragment);
        transaction.commit();
    }

    private void setListener() {
        rg.setOnCheckedChangeListener(this);
    }

    private void initView() {
        rg=(RadioGroup)findViewById(R.id.rg_main);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        FragmentTransaction transaction = manager.beginTransaction();
        switch (checkedId){
            case R.id.rb_main_first:
                FirstFragment firstFragment=new FirstFragment();
                transaction.replace(R.id.ll_main,firstFragment);
                break;
            case R.id.rb_main_seach:
                SeachFragment seachFragment=new SeachFragment();
                transaction.replace(R.id.ll_main,seachFragment);
                break;
            case R.id.rb_main_book:
                BookFraqment bookFraqment=new BookFraqment();
                transaction.replace(R.id.ll_main,bookFraqment);
                break;
            case R.id.rb_main_mine:
                MineFragment mineFragment=new MineFragment();
                transaction.replace(R.id.ll_main,mineFragment);
                break;
        }
        transaction.commit();
    }
}
